<template>
  <editor-layout class="edit" @ok="handleOk"  v-on="$listeners">
    <el-form :model="model" class="customForm" size="small" label-width="90px">
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="字段英文名" prop="prop">
            <el-input v-model="model.prop" placeholder="字段英文名" clearable/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="字段中文名" prop="label">
            <el-input v-model="model.label" placeholder="字段中文名" clearable/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="宽度" prop="label">
            <el-input v-model="model.width" placeholder="如: 100px 或 30%" clearable/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="对齐方式" prop="textAlign">
            <el-radio-group v-model="model.textAlign">
              <el-radio-button label="left">居左</el-radio-button>
              <el-radio-button label="center">居中</el-radio-button>
              <el-radio-button label="right">居右</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="是否隐藏" prop="textAlign">
            <el-switch v-model="model.hidden"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否是下标" prop="textAlign">
            <el-switch v-model="model.isIndex"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="颜色">
            <el-color-picker v-model="model.color" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="渲染函数">
        <monaco-editor
            v-model="model.render"
            language="javascript"
            style="height: 350px; overflow-y: hidden;"
        />
      </el-form-item>

    </el-form>

    <template v-slot:footer>
      <el-button v-show="model.id" type="danger" size="small" @click="handleDel">删除</el-button>
      <el-button size="small" @click="handleCancel">取消</el-button>
      <el-button size="small" type="primary" @click="handleOk">确定</el-button>
    </template>
  </editor-layout>
</template>

<script>
import monacoEditor from "@/components/common/monaco/monaco-editor";
import {funEval, tdRenderFunStr} from "@/utils/FunUtil";

export default {
  name: 'column-edit',
  cnName: 'column-edit',
  components: {
    monacoEditor
  },
  props: {
    data: Object
  },
  data() {
    return {
      model: {
        id: '',
        prop: '',
        label: '',
        width: '',
        textAlign: '',
        color: '',
        hidden: false,
        isIndex: false,
        render: tdRenderFunStr
      }
    }
  },
  methods: {
    init() {
      if(this.data) {
        const model = {...this.data}
        this.model = {
          ...model,
          render: model.render || tdRenderFunStr
        }
      }
    },
    handleCancel() {
      this.$emit('cancel')
    },
    handleDel() {
      this.$emit('on-ok', 0, this.model.id)
    },
    handleOk() {
      try {
        if(this.model.render) {
          funEval(this.model.render)
        }

        const param = {...this.model}
        if(!param.id) {
          param.id = new Date().getTime()
        }

        if(param.render.trim() === tdRenderFunStr.trim()) {
          delete param.render
        } else {
          param.render = param.render.trim()
        }

        this.$emit('on-ok', 1, param)
      } catch (e) {
        console.error(e)
        this.$message.warning('脚本格式有误,请查看控制台!')
      }
    }
  },
  mounted() {
    this.init()
  }
}
</script>

<style lang="less" scoped>
.column-edit {
}
</style>
